%This function conducts non-linear least squares computations for the
%robust heteroskedasticity-consistent standard errors for NLLS from the
%sandwich form
function [NLLSSandwich] = NLLSVarMtrxCalcs(J,R)

n = size(J,1);
ehat = R;
X = J;
QHat = (1/n)*(X'*X);
counter = 0;
OmegaHat = zeros(size(J,2));
while(counter<n)
    counter = counter + 1;
    OmegaHat = OmegaHat + ((ehat(counter,1))^2)*(X(counter,:)'*X(counter,:));
end
OmegaHat = (1/n) * OmegaHat;
Qinv = pinv(QHat);
NLLSSandwich = Qinv*OmegaHat*Qinv;

end